<?php
require_once '../../bbs/include/common.inc.php';
require_once '../../classes/include.php';

/*
session_start();
print_r($_SESSION);
print_r($_COOKIE);exit;
*/

//得到视频分类
$sql = "select * from topic_type where fup = 0 order by fid desc";
$topic_type_query = $db->query($sql);


if(!empty($_POST['add']))
{
	$uploaddir = "../../videofiles/";//设置文件保存目录 注意包含/
//$type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型
	
	//栏目(路径)
	$sort = $_POST['sort'];

	/*
	//获取文件后缀名函数
	function fileext($filename)
	{
		return substr(strrchr($filename, '.'), 1);
	}
	*/
	
	/*
	$a=strtolower(fileext($_FILES['file']['name']));
	//判断文件类型
	if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type))
	{
		$text=implode(",",$type);
		echo "您只能上传以下类型文件: ",$text,"<br>";
	}
	//生成目标文件的文件名
	else
	{
	*/
		$filename = explode(".",$_FILES['file']['name']);
		do
		{
			$filename[0] = md5(time()); 
			$video_name = implode(".",$filename);
			//$uploadfile = $uploaddir.$sort.'/'.$video_name;
			$uploadfile = $uploaddir.'/'.$video_name;
		}
		while(file_exists($uploadfile));
		
		if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
		{		
			$title			= $_POST["title"];
			$need_scores	= $_POST["need_scores"];
			$deduct_scores	= $_POST["deduct_scores"];			
			$describes		= htmlspecialchars( stripslashes( $_POST['describes'] ) );
			$upload_time	= date('Y-m-d H:i:s', time());
			$author			= 1;
		
			$sql = "insert into video (title, sort, video_name, describes, author, need_scores, deduct_scores, upload_time ) values
					('".$title."', '".$sort."' ,'".$video_name."' ,'".$describes."' ,'".$author."' ,'".$need_scores."' ,'".$deduct_scores."' ,'".$upload_time."')";
			$query = $db->query($sql);
			
			echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
			echo "<script>";
			echo "alert('视频增加成功!');";
			echo "location.href='list.php';";
			echo "</script>";
			exit;			
		}
		else
		{
			echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
			echo "<script>";
			echo "alert('视频上传失败!');";
			echo "location.href='list.php';";
			echo "</script>";
			exit;
		}
	//}
}
?>

<html><head>
<title></title>
<meta name="robots" content="noindex, nofollow" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../ckeditor.js"></script>
<script type="text/javascript" src="../../js/jquery.js"></script>
<link rel="stylesheet" href="../../bbs/images/admincp/admincp.css" type="text/css" media="all" /> 
<script type="text/javascript">
<!--
function ChangeLanguage( languageCode )
{
	window.location.href = window.location.pathname + "?" + languageCode ;
}

function Check(frm)
{
	if(frm.title.value == ""){
		alert("请填写视频标题!");
		frm.title.focus();
		return false;
	}
	
	if(frm.need_scores.value == ""){
		alert("请填写所需积分!");
		frm.need_scores.focus();
		return false;
	}
	
	if(frm.deduct_scores.value == ""){
		alert("请填写扣除积分!");
		frm.deduct_scores.focus();
		return false;
	}
	
	if(isNaN(frm.need_scores.value)){
		alert("所需积分填写有误!");
		frm.need_scores.focus();
		return false;
	}
	
	if(isNaN(frm.deduct_scores.value)){
		alert("扣除积分填写有误!");
		frm.deduct_scores.focus();
		return false;
	}
	
	if(frm.file.value == ""){
		alert("您还没有上传视频!");
		frm.file.focus();
		return false;
	}
	
	
	
	var wait_img = "<img src='../../images/ajax_wait.jpg'>";
	$('#wait').html(wait_img);
	
	/*if(frm.datetime.value == ""){
		alert("请填写新闻日期!");
		frm.datetime.focus();
		return false;
	}
	if(!/^[0-9a-zA-Z_\u4E00-\u9FA5]*$/.test(frm.title.value)){
		alert("请正确填写文章标题,只能由中文、数字、字母和下划线组成!");
		frm.title.focus();
		return false;	
	}*/
	return true;
}
//-->
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form enctype="multipart/form-data" action="add.php" method="post" onsubmit="return Check(this)">
  <table cellspacing=1 cellpadding=3 width="100%" align=center bgcolor="#CBDFF3" border=0>
    <tr class="tableheader"> 
      <td colspan="2">&nbsp;&nbsp;增加视频</td>
    </tr>
    <tr class="tablecontent"> 
      <td width="100" align="left">&nbsp;&nbsp;视频标题:</td>
      <td>
        <input type="text" name="title" maxlength="255" class="FormText" size="100">
      </td>
    </tr>
    <tr class="tablecontent"> 
      <td width="100" align="left">&nbsp;&nbsp;所属分类:</td>
      <td>
        <select name="sort">
        	<?php
        	while($video_menu_info = $db->fetch_array($topic_type_query)) {
        		
        		echo "<option value=".$video_menu_info["fid"].">".$video_menu_info['name']."</option>";
        		
        		/*
        		$fid = $video_menu_info['fid'];
        		if($fid != 0)
        		{
        			$sql = "select * from topic_type where fup = '".$fid."' order by fid desc";
					$topic_info = $db->query($sql);
					
					while($video_menu_infos = $db->fetch_array($topic_info)) {
						echo "<option value=".$video_menu_infos["fid"].">&nbsp;&nbsp;&nbsp;&nbsp;".$video_menu_infos['name']."</option>";
					}
        		}
				*/
			}
			?>
        </select>
      </td>
    </tr>
    <tr class="tablecontent"> 
      <td width="100" align="left">&nbsp;&nbsp;描&nbsp;&nbsp;&nbsp;&nbsp;述:</td>
      <td>
        <textarea name='describes'></textarea>
      </td>
    </tr>
    <tr class="tablecontent"> 
      <td width="100" align="left">&nbsp;&nbsp;所需积分:</td>
      <td>
        <input type="text" name="need_scores" class="FormText" >
      </td>
    </tr>
    <tr class="tablecontent"> 
      <td width="100" align="left">&nbsp;&nbsp;扣除积分:</td>
      <td>
        <input type="text" name="deduct_scores" class="FormText" >
      </td>
    </tr>
    <!-- <tr class="tablecontent"> 
      <td width="100" align="left">&nbsp;&nbsp;新闻日期:</td>
      <td>
        <input type="text" name="datetime" maxlength="50" class="FormText" size="100">
      </td>
    </tr> -->
	<tr class="tablecontent"> 
		<td align="left">&nbsp;&nbsp;上传视频:</td>
		<td>
    		<input name='file' type='file'>
    	</td>
    </tr>
  </table>
  <BR>
  <div id='wait'></div>
  <BR>
  <table cellspacing=1 cellpadding=3 width="95%" align=center bgcolor="#FFFFFF" border=0>
    <tr> 
      <td colspan="2" align="center"> 
        <input type="submit" id="add_button" name="add" value="增加">
        <input type="button" value="返回" onclick="javascript:location.href='./list.php';">
      </td>
    </tr>
  </table>
</form>
</body>
</html>